$.Class("Hyip.Grid",
// static properties
{         
    addParams : function(e, params)
    {
        e.sender.transport.options.read.data = params;
    }    
},
// prototype properties
    {
        // constructor function
        init: function(id) {
            this.id = id;
            
            this.dataSource = this.createDataSource();
            this.createGrid();     
            this.initEvents(id);
        },
        createDataSource: function()
        {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "/hyip1/search",
                        dataType: "json",
                        type: "post",
                        data : {
                            
                        }
                    },
					update: {
						url: "/hyip1/update",
						dataType: "json",
						type: "post",
					},
					create: {
						url: "/hyip1/create",
						dataType: "json",
						type: "post"						
					},
					destroy: {
						url: "/hyip1/delete",
						dataType: "json",
						type: "post"
					},
					parameterMap: function(options, operation) {
						if (operation == "create" || operation == "update"){
							var model = options.models[0];
							if($("#startDate").val() == "") model.startDate = null;
							else model.startDate = $("#startDate").val();
							if($("#endDate").val() == "") model.endDate = null;										
							else model.endDate = $("#endDate").val();
							if($("#startDomain").val() == "") model.startDomain = null;										
							else model.startDomain = $("#startDomain").val();
							if($("#endDomain").val() == "") model.endDomain = null;										
							else model.endDomain = $("#endDomain").val();
							if($("#joinedDate").val() == "") model
							.joinedDate = null;
							else model.joinedDate = $("#joinedDate").val();
														                     
							$("input", "#paymentMethod").each(function(){
								if(this.checked)
									model[this.id] = 1;
								else
									model[this.id] = 0;
							});	  
							
							$("input", "#feature").each(function(){
								if(this.checked)
									model[this.id] = 1;
								else
									model[this.id] = 0;
							});	                            
                                                        
							delete(model.joinedDateNumber);
							delete(model.rate);
							delete(model.lastPaid);
							delete(model.lastWithdraw);
							delete(model.duration);
							delete(model.grid_index);
						}
						
						return options;
					}
                },
				change: function (e, data) {									
					if(e.action == "sync")
						$("#grid").data("kendoGrid").dataSource.read();	
				},
				error: function (xhr, error) {
					//console.debug(xhr); console.debug(error);
				},				
				batch: true,
                pageSize: 25,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: "results",
                    total: function(data) {
                        return data.total;
                    },
					model: {
						id: "id",
						fields: {
							id: { editable: false},
							domain: { validation: { required: true }},
							ip: { validation: { required: true }},
							planType: {defaultValue:'Very short'},
							profitType: {defaultValue:'Low'},
							status: {defaultValue:'New'},
							startDate: {},
							endDate: {},							
							startDomain: {},
							endDomain: {},
							joinedDate: {},				
							plan: {},
							joinedDateNumber:{},
							lastPaid: {},
							lastWithdraw: {},
							rate: {},
                                                        sslType: {defaultValue:0},
                                                        budget:{}
						}
					}
                },							
                requestStart: function(e) {   
					var data = {
                        domain : $("#_domain").val(),
                        status : $("#_status").val(),
                        planType : $("#_planType").val(),
                        ip : $("#_ip").val(),
                        monitorDomain : $("#_monitorDomain").val(),
                        paymentMethod : $("[name='_paymentMethod']:checked").val(),
                        isAjax : true
                    };
                    
                    Hyip.Grid.addParams(e, data);
                }
            });

            return dataSource;
        }, 
        createGrid : function()
        {
            $("#" + this.id).kendoGrid({
	        dataSource: this.dataSource,
	        toolbar: ["create","Reset"],
	        //height: 500,
	        filterable: false,	        
	        pageable: true,
	        columns: [{
                field:"id",
                title:"ID",
                width: 50
            },{
                field:"grid_index",
                title:" ",
                width: 40
            },{
                field:"domain",
                title:"Domain",
                width: 180,
                template: '<a href="" onclick="return showWindowPlan(${id});">${domain}</a>'
            }/*,{
            	title:"Joined date",
                field:"joinedDate",         
                width: 100
            }*/,{
            	title:"Joined",
                field:"joinedDateNumber",         
                width: 70,				
                template: '${joinedDateNumber} days'
            },{
            	title:"Paid",
                field:"lastPaid",         
                width: 50,
                template: '<a href="" onclick="return Hyip.updateLastPaid(${id});">${lastPaid}</a>'
            },{
            	title:"Wdraw",
                field:"lastWithdraw",         
                width: 60,
                template: '<a href="" onclick="return Hyip.updateLastWithdraw(${id});">${lastWithdraw}</a>'
            },{
            	title:"Instant",
                field:"isPayInstant",         
                width: 60                
            },{
            	title:"Rate",
                field:"rate",         
                width: 115,
                template: '<div id="rate_${id}" itemID="${id}" value="${rate}" class="rate"></div>'
            },{
            	title:"Budget $",
                field:"budget",         
                width: 90,
                template: '<a href="" onclick="return Hyip.viewBudget(${id});">$${budget}</a>'
            },{
                field:"status",
                title:"Status",
                width: 75
            },{
            	title:"Start date",
                field:"startDate",
                width: 100
            },{
            	title:"Plan time",
                field:"planTime",         
                width: 75
            },{
            	title:"Life time",
                field:"lifeTime",         
                width: 75
            }/*,{
            	title:"End date",
                field:"endDate",
                width: 100
            }*//*,{
            	title:"Start domain",
                field:"startDomain",
                width: 100
            },{
            	title:"End domain",
                field:"endDomain",
                width: 100
            }*/,{
                field:"planType",
                title:"Plan type",
                width: 100
            },{
                field:"profitType",
                title:"Profit",
                width: 75
            },{
            	title:"IP",
                field:"ip",
                width: 160  
            },{ 
                command: ["edit","destroy"], title: "", width: 170 
            },{
            	title:"",
                field:"",   
                template: ''
            }],
            editable: {
                mode: "popup",
                template: $("#teamEditorTemplate").html(),
                update: true,
                destroy: true,				
                confirmation: "Are you sure you want to remove this item ?",
                window:{
                        width:730,
                        center: true
                }
            },
            edit: function(e)
            {		
				if(e.model.isLibertyReverse == 1) $("#isLibertyReverse").attr("checked","checked");
				if(e.model.isPerfectMoney == 1) $("#isPerfectMoney").attr("checked","checked");
				if(e.model.isEgoPay == 1) $("#isEgoPay").attr("checked","checked");
				if(e.model.isSolidTrustPay == 1) $("#isSolidTrustPay").attr("checked","checked");
				if(e.model.isBankWire == 1) $("#isBankWire").attr("checked","checked");
				if(e.model.isPayza == 1) $("#isPayza").attr("checked","checked");
				if(e.model.isWebMoney == 1) $("#isWebMoney").attr("checked","checked");
				if(e.model.isInterKassa == 1) $("#isInterKassa").attr("checked","checked");
				if(e.model.isAlfaBank == 1) $("#isAlfaBank").attr("checked","checked");
				if(e.model.isPaypal == 1) $("#isPaypal").attr("checked","checked");
				if(e.model.isAlertPay == 1) $("#isAlertPay").attr("checked","checked");
				if(e.model.isTraceable == 1) $("#isTraceable").attr("checked","checked");
				if(e.model.isCreditCard == 1) $("#isCreditCard").attr("checked","checked");
				
				if(e.model.isDedicatedServer == 1) $("#isDedicatedServer").attr("checked","checked");
				if(e.model.isDDOS == 1) $("#isDDOS").attr("checked","checked");
				if(e.model.isSSL == 1) $("#isSSL").attr("checked","checked");
				if(e.model.isUniqueDesign == 1) $("#isUniqueDesign").attr("checked","checked");
				if(e.model.isUniqueScript == 1) $("#isUniqueScript").attr("checked","checked");
				if(e.model.isGCScript == 1) $("#isGCScript").attr("checked","checked");
				if(e.model.isSupportOnline == 1) $("#isSupportOnline").attr("checked","checked");
				if(e.model.isPayInstant == 1) $("#isPayInstant").attr("checked","checked");
				if(e.model.isPay7DaysAWeek == 1) $("#isPay7DaysAWeek").attr("checked","checked");
            },
            save: function(e) {
                e.model.dirty = true;
            },
            sortable:true,
            selectable: 'row',
            dataBound: function(e) {                
                $(".rate").each(function(){                   
                    var itemID = $(this).attr("itemID"); 
                    var rate = $(this).attr("value");
                    
                    if(rate == null) rate = 0;
                    
                    $('#' + this.id).raty({
                        half  : true,
                        score : rate,
                        click: function(score, evt) {
                            $.ajax({
                                type: 'post',
                                url: '/hyip1/updateRate',
                                data: {
                                    id : itemID,
                                    rate : score
                                },
                                dataType: 'json',
                                success: function(obj){

                                },
                            });
                        }
                    });
                });
            }
        });	
        },
        initEvents : function(id)
        {           
            $(".k-grid-Reset", "#" + this.id).click(function(){
                $("#" + id).data("kendoGrid").dataSource.read();	// Reset grid
                return false;
            });	
        },
        refresh : function()
        {
            $("#" + this.id).data("kendoGrid").dataSource.read();	// Reset grid
        }
})

Hyip.updateLastPaid = function(id)
{
    if(confirm("Have they paid ?"))
    {
        $.ajax({
            type: 'post',
            url: '/hyip1/updateLastPaid',
            data: {
                id : id
            },
            dataType: 'json',
            success: function(obj){
                $("#grid").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}

Hyip.updateLastWithdraw = function(id)
{
    if(confirm("Have you received ?"))
    {
        $.ajax({
            type: 'post',
            url: '/hyip1/updateLastWithdraw',
            data: {
                id : id
            },
            dataType: 'json',
            success: function(obj){
                $("#grid").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}

Hyip.viewBudget = function(id)
{
	try{
		Budget.hyipId = id;
		$("#tabstrip").data("kendoTabStrip").select(1);
		gridBudget.refresh();
	}
	catch(ex)
	{
		alert("Err: " + ex);
	}
    return false;
}